{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 使用numpy.meshgrid() 创建二维网格数组\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码的数学核心是创建一个二维网格，其中 $x_1$ 和 $x_2$ 分别表示平面上的两个变量。我们可以从数学角度详细描述其作用如下：\n",
    "\n",
    "首先，$x_1$ 和 $x_2$ 是从 $-4$ 到 $4$ 之间等间距取值的数列，步长为 $1$：\n",
    "\\[\n",
    "x_1 = x_2 = \\{-4, -3, -2, -1, 0, 1, 2, 3, 4\\}\n",
    "\\]\n",
    "这两个数列定义了一系列离散的坐标点，它们将用于构造一个网格。\n",
    "\n",
    "**网格生成（Meshgrid）**\n",
    "\n",
    "使用 $\\text{np.meshgrid}(x_1, x_2)$，代码生成了两个矩阵 $xx_1$ 和 $xx_2$，它们的形状均为 $(9, 9)$。这些矩阵的作用是形成一个二维坐标网格，其中：\n",
    "- $xx_1$ 的每一行都与 $x_1$ 相同，表示 $x_1$ 方向上的坐标重复扩展。\n",
    "- $xx_2$ 的每一列都与 $x_2$ 相同，表示 $x_2$ 方向上的坐标重复扩展。\n",
    "\n",
    "用数学表示，网格上的每个点 $(x_1, x_2)$ 由：\n",
    "\\[\n",
    "xx_1(i, j) = x_1(j), \\quad xx_2(i, j) = x_2(i)\n",
    "\\]\n",
    "其中 $i, j$ 分别是矩阵的行索引和列索引。\n",
    "\n",
    "最终，$xx_1$ 和 $xx_2$ 可以被看作是二维平面上 $x_1$ 和 $x_2$ 轴上的坐标网格，它们常用于绘制等高线图、三维曲面图或者计算某些二维函数值。\n",
    "\n",
    "**示例网格点**\n",
    "例如，$(x_1, x_2)$ 的部分取值：\n",
    "\\[\n",
    "\\begin{aligned}\n",
    "xx_1 &= \n",
    "\\begin{bmatrix}\n",
    "-4 & -3 & -2 & -1 & 0 & 1 & 2 & 3 & 4 \\\\\n",
    "-4 & -3 & -2 & -1 & 0 & 1 & 2 & 3 & 4 \\\\\n",
    "\\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots \\\\\n",
    "-4 & -3 & -2 & -1 & 0 & 1 & 2 & 3 & 4\n",
    "\\end{bmatrix} \\\\\n",
    "xx_2 &= \n",
    "\\begin{bmatrix}\n",
    "-4 & -4 & -4 & -4 & -4 & -4 & -4 & -4 & -4 \\\\\n",
    "-3 & -3 & -3 & -3 & -3 & -3 & -3 & -3 & -3 \\\\\n",
    "\\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots & \\vdots \\\\\n",
    "4 & 4 & 4 & 4 & 4 & 4 & 4 & 4 & 4\n",
    "\\end{bmatrix}\n",
    "\\end{aligned}\n",
    "\\]\n",
    "这些矩阵的每个元素对应于二维平面上的一个坐标点 $(x_1, x_2)$，用于后续的数学计算或可视化。\n",
    "\n",
    "总结来说，这段代码的作用是构建一个二维网格，它定义了在 $[-4,4] \\times [-4,4]$ 这个离散点集上的坐标点分布，为进一步计算函数值或绘图提供基础。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "398d0a9c-4884-4750-a4eb-f234428eb21f",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "592d9f86-794b-4b36-89c3-099726910e65",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3b97692-7fe2-4f0d-badf-48d46849d7d6",
   "metadata": {},
   "source": [
    "## 一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ba06a502-610c-49bf-9200-02bb7c7736f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成 x1 和 x2 的一维数组，范围 [-4, 4]，步长为 1\n",
    "x1 = np.arange(-4, 5, 1)\n",
    "x2 = np.arange(-4, 5, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63975350-6c23-4a8e-981a-a8bd775c0710",
   "metadata": {},
   "source": [
    "## 二维网格数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e87412bc-0d80-43d5-8998-133654a75faf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成网格\n",
    "xx1, xx2 = np.meshgrid(x1, x2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "954b7525-dcef-47cb-af13-40c741ca3891",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-4, -3, -2, -1,  0,  1,  2,  3,  4],\n",
       "       [-4, -3, -2, -1,  0,  1,  2,  3,  4],\n",
       "       [-4, -3, -2, -1,  0,  1,  2,  3,  4],\n",
       "       [-4, -3, -2, -1,  0,  1,  2,  3,  4],\n",
       "       [-4, -3, -2, -1,  0,  1,  2,  3,  4],\n",
       "       [-4, -3, -2, -1,  0,  1,  2,  3,  4],\n",
       "       [-4, -3, -2, -1,  0,  1,  2,  3,  4],\n",
       "       [-4, -3, -2, -1,  0,  1,  2,  3,  4],\n",
       "       [-4, -3, -2, -1,  0,  1,  2,  3,  4]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9a34e318-63f0-416c-b364-c35b1ed64a71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(9, 9)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "eed8c293-8ede-4c9b-a715-049f9f24ff43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx1.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7eb025ba-bf5e-424b-8332-4d7682dd8f52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-4, -4, -4, -4, -4, -4, -4, -4, -4],\n",
       "       [-3, -3, -3, -3, -3, -3, -3, -3, -3],\n",
       "       [-2, -2, -2, -2, -2, -2, -2, -2, -2],\n",
       "       [-1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "       [ 0,  0,  0,  0,  0,  0,  0,  0,  0],\n",
       "       [ 1,  1,  1,  1,  1,  1,  1,  1,  1],\n",
       "       [ 2,  2,  2,  2,  2,  2,  2,  2,  2],\n",
       "       [ 3,  3,  3,  3,  3,  3,  3,  3,  3],\n",
       "       [ 4,  4,  4,  4,  4,  4,  4,  4,  4]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "69d33c20-60a6-4217-998a-2b5dff3e4f41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(9, 9)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3857e1c9-ae1b-410f-9bcf-e925b3432eb3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx2.ndim"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fbd2e7e-62b5-4f02-bba1-93c0081a2318",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
